package me.passtime.projt;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.*;
import me.passtime.projt.account.ChangePasswordPage;
import me.passtime.projt.account.RetrievePasswordStepOnePage;
import me.passtime.projt.fee.PaymentPage;
import me.passtime.projt.ivr.BindCardPage;
import me.passtime.projt.ivr.UnBindCardPage;
import me.passtime.projt.query.*;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.SystemUtils;
import me.passtime.projt.util.UserHolder;

import java.net.ContentHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午5:02
 */
public class Home extends Activity implements View.OnClickListener {
    private static final String TAG = "PROJ-T";
    private GridView mGridView;
    private List<Map<String, Object>> gridItems = new ArrayList<Map<String, Object>>();
    private Handler mHandler;
    private SimpleAdapter adapter;

    private Button mUserInfo;
    private Context content;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        content = this;
        mUserInfo = (Button) this.findViewById(R.id.person_btn);
        mUserInfo.setText(UserHolder.phoneNumber+"\r\n"+UserHolder.userName);
        initGridViewData();
        initGridViewGUI();
        this.findViewById(R.id.home_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.home_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.home_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.home_exit).setOnClickListener(this);

    }

    private void initGridViewData() {


        Map<String, Object> item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "充值缴费");
        item.put("itemId", 0);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "当月话费");
        item.put("itemId", 1);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "套餐余额");
        item.put("itemId", 2);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "历史账单");
        item.put("itemId", 3);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "上网流量");
        item.put("itemId", 4);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "详单查询");
        item.put("itemId", 5);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "套餐变更");
        item.put("itemId", 6);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "积分查询");
        item.put("itemId", 7);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "绑定银行卡");
        item.put("itemId", 8);
        gridItems.add(item);

        item = new HashMap<String, Object>();
        item.put("ItemImage", R.drawable.upomp_bypay_icon);
        item.put("ItemText", "解除绑定");
        item.put("itemId", 9);
        gridItems.add(item);


    }

    /**
     * 初始化 grid View
     */
    private void initGridViewGUI() {
        adapter = new SimpleAdapter(this, gridItems, R.layout.grid_view_item, new String[]{"ItemImage", "ItemText"}, new int[]{R.id.ItemImage, R.id.ItemText,});
        mGridView = (GridView) findViewById(R.id.gridView);
        mGridView.setAdapter(adapter);
        mGridView.setOnItemClickListener(new GridListViewClickedListener());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.home_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.home_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.home_exit:
                AlertDialogUtils.showYesNoDiaLog(this,"是否退出系统?",new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
        }
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.logoutAndJumpToMainPage(this);
    }


    class GridListViewClickedListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String itemId = gridItems.get(i).get("itemId").toString();
            Log.d(TAG, itemId);
            if (itemId.equals("0")) {
                PageJumpUtils.jump(content, PaymentPage.class);
            } else if (itemId.equals("1")) {
                PageJumpUtils.jump(content, QueryCurrentMonthPage.class);
            } else if (itemId.equals("2")) {
                PageJumpUtils.jump(content, QueryPackageRemainPage.class);
            } else if (itemId.equals("3")) {
                PageJumpUtils.jump(content, QueryHistoryPage.class);
            } else if (itemId.equals("4")) {
                PageJumpUtils.jump(content, QueryAvailableBalancePage.class);
            } else if (itemId.equals("5")) {
                PageJumpUtils.jump(content, QueryPaymentPage.class);
            }else if (itemId.equals("6")) {
//                PageJumpUtils.jump(content, ChangePasswordPage.class);
            }else if (itemId.equals("7")) {
//                PageJumpUtils.jump(content, RetrievePasswordStepOnePage.class);
            }else if (itemId.equals("8")) {
                PageJumpUtils.jump(content, BindCardPage.class);
            }else if (itemId.equals("9")) {
                PageJumpUtils.jump(content, UnBindCardPage.class);
            }
        }
    }
}
